package Gui;

import java.util.ArrayList;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import auth.User;

public class Main {
	private Display display = new Display();
	private Shell shell;
	private final Button loginBt;
	private final Button logoutBt;
	private final Button regBt;
	private ArrayList<Shell> listOfActiveShells = new ArrayList();
	private Composite browser;
	private boolean isLoggedIn = false;
	private User mUser = null;
	private boolean playlistActive = false;
	private boolean searchActive = false;
	public Main(){
		shell = new Shell(display);
		loginBt = new Button(shell,SWT.PUSH);
		logoutBt = new Button(shell,SWT.PUSH);
		regBt = new Button(shell,SWT.PUSH);
		createMain(this);
	}

	private void createMain(final Main main) {	 
		disableLogout();
		shell.setText("IMusic");
		Rectangle monitor_bounds = shell.getMonitor().getBounds();
		shell.setSize(new Point(monitor_bounds.width/2,
				monitor_bounds.height/2));
		shell.open();
		//Set the background image
		shell.setBackgroundImage(new Image(display, "bgs/bg1.jpg"));
		//Top menu
		Menu topMenu = new Menu(shell,SWT.BAR);
		shell.setMenuBar(topMenu);
		MenuItem gotoPlaylist = new MenuItem(topMenu,SWT.CASCADE);
		MenuItem gotoSearch = new MenuItem(topMenu,SWT.CASCADE);
		MenuItem closeBt = new MenuItem(topMenu,SWT.CASCADE);
		gotoPlaylist.setText("Go to Playlist");
		gotoSearch.setText("Go to Search");
		closeBt.setText("Close IMusic");
		//Login button
		loginBt.setText("Login");
		loginBt.setSize(100,20);
		loginBt.setLocation(shell.getSize().x-loginBt.getSize().x-40,40);
		//Register Button
		regBt.setText("Register");
		regBt.setSize(100,20);
		regBt.setLocation(shell.getSize().x-regBt.getSize().x-40,80);
		//Logout Button
		logoutBt.setText("Logout");
		logoutBt.setSize(100,20);
		logoutBt.setLocation(shell.getSize().x-regBt.getSize().x-40,120);
		//Actions
		loginBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						disableLogin();
						disableReg();
						Login login = new Login(shell,main);
					}
				}
		);
		regBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						disableLogin();
						disableReg();
						Register register = new Register(shell, main);
					}
				}
		);
		logoutBt.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						mUser.saveList();
						browser.dispose();
						setPlaylistNotActive();
						setSearchNotActive();
						disableLogout();
						enableLogin();
						enableReg();
						setNotLogged();
						closeActiveShells();
					}
				}
		);
		gotoPlaylist.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						if(isLoggedIn){
							if(!isPlaylistActive()){
								closeActiveShells();
								setPlaylistActive();
								setSearchNotActive();
								Playlist playlist = new Playlist(null, shell, mUser, null, main);
							}else{
								findShell("playlistShell").setFocus();
							}
						}else{
							MessageDialog.Error(shell, "Error", "You must be logged in to enter the playlist!");
						}
					}
				}
		);
		gotoSearch.addSelectionListener(
				new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						if(isLoggedIn){
							if(!isSearchActive()){
								setSearchActive();
								Search search = new Search(shell, getUser(), main);
							}else{
								findShell("searchShell").setFocus();
							}
						}else{
							MessageDialog.Error(shell, "Error", "You must be logged in to enter the search!");
						}
					}
				}
		);
		
		closeBt.addSelectionListener(
				new SelectionAdapter() {					
					public void widgetSelected(SelectionEvent e) {
						MessageBox SaveChangesBox = new MessageBox(shell, SWT.ICON_WARNING| SWT.YES| SWT.NO);
						SaveChangesBox.setText("IMusic");
						SaveChangesBox.setMessage("Are you sure you want to close IMusic?");
						int Answer = SaveChangesBox.open();
						if(Answer == SWT.YES){
							try{
								mUser.saveList();
								closeActiveShells();
								shell.dispose();
							}catch(Exception e2){ 
								//If user presses no in exit window, do nothing
							}

						}
					}
				}
		);
		
		Listener listener = new Listener () {
			public void handleEvent (Event e) {
				switch (e.type) {
				case SWT.Close:
					MessageBox SaveChangesBox = new MessageBox(shell, SWT.ICON_WARNING| SWT.YES| SWT.NO);
					SaveChangesBox.setText("IMusic");
					SaveChangesBox.setMessage("Are you sure you want to close IMusic?");
					int Answer = SaveChangesBox.open();
					if(Answer == SWT.YES){
						try{
							mUser.saveList();
							closeActiveShells();
							shell.dispose();
						}catch(Exception e2){}
					}else{
						e.doit = false;
					}
					break;
				default:
		        }
		   }
		};

		shell.addListener (SWT.Close, listener);

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) display.sleep ();
		}
		display.dispose();
	}
	public Shell findShell(String name){
		for(Shell sH:listOfActiveShells){
			if(getShellName(sH.toString()).equals(name)){
				return sH;
			}
		}
		return null;
	}
	private String getShellName(String str){
		int i;
		String name="";
		int foundB = 0;
		for(i=0;i<str.length();i++){
			if(str.charAt(i)=='{' || str.charAt(i)=='}'){
				foundB++;
				continue;
			}
			if(foundB == 1){
				name+=str.charAt(i);
			}
		}
		name=name.toLowerCase();
		name+="Shell";
		return name;
	}
	public void enableLogin(){
		loginBt.setEnabled(true);
	}
	public void disableLogin(){
		loginBt.setEnabled(false);
	}
	public void enableLogout(){
		logoutBt.setEnabled(true);
	}
	public void disableLogout(){
		logoutBt.setEnabled(false);
	}
	public void enableReg(){
		regBt.setEnabled(true);
	}
	public void disableReg(){
		regBt.setEnabled(false);
	}
	public Display getDisplay() {
		return display;
	}
	public Shell getShell() {
		return shell;
	}
	public void setUser(User mUser){
		this.mUser = mUser;
	}
	public User getUser(){
		return mUser;
	}
	public void setLogged(){
		isLoggedIn = true;
	}
	public void setNotLogged(){
		isLoggedIn = false;
	}
	public void setPlaylistActive(){
		this.playlistActive = true;
	}
	public void setPlaylistNotActive(){
		this.playlistActive = false;
	}
	public boolean isPlaylistActive(){
		return this.playlistActive;
	}
	public void setSearchActive(){
		this.searchActive = true;
	}
	public void setSearchNotActive(){
		this.searchActive = false;
	}
	public boolean isSearchActive(){
		return this.searchActive;
	}
	public void addShell(Shell shell){
		listOfActiveShells.add(shell);
	}
	public void removeShell(Shell shell){
		listOfActiveShells.remove(shell);
	}
	public void closeActiveShells(){
		for(Shell Sh:listOfActiveShells){
			Sh.dispose();
		}
	}
	public void setBro(Composite c){
		this.browser = c;
	}
}
